﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AssociationCorpLogic;

namespace AssociationCorpGUI
{
    //Delegates:
    public delegate void WholeWordUnlockedEventHandler(object sender, WholeWordUnlockedEvent e);

    public partial class guiAssociation : UserControl
    {
        //Variables:
        private Association association;

        //Properties:
        public Point Middle { get { return new Point(this.Left + this.Width / 2, this.Top + this.Height / 2); } }

        //Constructors:
        public guiAssociation()
        {
            InitializeComponent();
        }
        public guiAssociation(Association association) : this()
        {
            this.association = association;
            this.lblWord.Text = this.association.Word;
        }

        //Events:
        public event WholeWordUnlockedEventHandler WholeWordUnlocked;
        protected virtual void OnWholeWordUnlocked(WholeWordUnlockedEvent e)
        {
            if (WholeWordUnlocked != null)
            {
                WholeWordUnlocked(this, e);
            }
        }

        //Methods:
        private void lblWord_DoubleClick(object sender, EventArgs e)
        {
            guiAssociation_DoubleClick(sender, e);
        }
        private void guiAssociation_DoubleClick(object sender, EventArgs e)
        {
            if (!this.association.Uncovered)
            {
                dlgTryUnlock dlg = new dlgTryUnlock(association.Word.Length);
                //listen for tries to unlock the association
                this.AddTryUnlockListener(dlg);
                //show dialog
                dlg.Location = new Point((this.Left + this.Width / 2) - (dlg.Left + dlg.TextboxWidth / 2), (this.Top + this.Height / 2) - (dlg.Top + dlg.Height / 2));
                this.Parent.Controls.Add(dlg);
                dlg.BringToFront();
                dlg.Focus();
            }
        }

        /// <summary>
        /// Adds a listener for the event that the user tries to unlock 
        /// </summary>
        /// <param name="dlg"></param>
        private void AddTryUnlockListener(dlgTryUnlock dlg)
        {
            dlg.TryUnlock += new TryUnlockEventHandler(TryUnlock);
        }
        private void TryUnlock(object sender, UnlockAssociationEventArgs e)
        {
            this.association.TryUnlock(e.Word);
            this.lblWord.Text = this.association.Word;

            if (this.association.Uncovered)
            {
                //TODO: generate message about succeding to unlock word
                MessageBox.Show("Grattis!");
                //create new guiassociations on the gameboard
                OnWholeWordUnlocked(new WholeWordUnlockedEvent(this.association));
            }

            //dlgTryUnlock dlg = (dlgTryUnlock)sender;
            //this.RemoveTryUnlockListener(dlg);
        }
    }
}
